package com.soa.zuul.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.soa.zuul.entity.GatewayHttpApi;
import com.soa.zuul.entity.GatewayService;
import com.soa.zuul.util.Json;





@Service
@Transactional
public class GatewayServiceService {
	public final static Logger logger = LoggerFactory.getLogger(GatewayServiceService.class);
	@PersistenceContext
	private EntityManager entityManager;
	@Resource
	StringRedisTemplate stringRedisTemplate;
	private Session session;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Session getSession() {
		return this.getEntityManager().unwrap(org.hibernate.Session.class);
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	
	/**
	 * 根据url查询服务对象 需要修改为restful格式
	 * @param path
	 * @return
	 */
	public GatewayService getGatewayServiceByPath(String path,String apiid) {
		GatewayService result=null;
		PathMatcher matcher = new AntPathMatcher();
		String pathpattern=GatewayService.class.getSimpleName()+":path|"+apiid+":*";
		List<String> gatewayServiceJsonList = null;
		try {
			List<String> keys = new ArrayList<String>(stringRedisTemplate.keys(pathpattern));
			gatewayServiceJsonList = stringRedisTemplate.opsForValue().multiGet(keys);
		} catch (Exception e) {
			logger.info("redsi读取GatewayHttpApi异常:"+e.getMessage());
			gatewayServiceJsonList = null;
			e.printStackTrace();
		}
		//redis中查出了数据从redis里面取
		if(gatewayServiceJsonList!=null && gatewayServiceJsonList.size()>0 ){
			for (String gatewayServiceJson : gatewayServiceJsonList) {
				GatewayService gatewayService = Json.GSON.fromJson(gatewayServiceJson,GatewayService.class);
				if (matcher.match(gatewayService.getPath(), path)) {
					result = gatewayService;
					break;
				}
			}
		}
		if (result==null){// redis 里面没有，则去数据中查,查到了同步到redis里面
			Criteria serviceCriteria = this.getSession().createCriteria(GatewayService.class);
			serviceCriteria.add(Restrictions.eq("apiId", apiid));
			List<GatewayService> gatewayServiceList = serviceCriteria.list();
			for (GatewayService gatewayService2 : gatewayServiceList) {
				if (matcher.match(gatewayService2.getPath(), path)) {
					result = gatewayService2;
					String pathkey=GatewayService.class.getSimpleName()+":path|"+apiid+":"+path;
					stringRedisTemplate.opsForValue().set(
							pathkey,
							Json.GSON.toJson(result));
					break;
				}
			}
		}
		return result;
	}
}
